<?php
define('ROOT_PATH', dirname(__FILE__));
set_include_path(get_include_path() . PATH_SEPARATOR . ROOT_PATH);

define('SITE_URL', 'http://'.$_SERVER['HTTP_HOST']);
define('SITE_URL_S', SITE_URL);

require_once ROOT_PATH.'/Library/__init__.php';

require_once ROOT_PATH.'/config.php';

// Cookie::setDomain('.xxxx.com');
Database::$instance = Database::connect(Config::get('db'));
Database::$instance->initialization = array(
    'SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary',
    "SET sql_mode=''"
);

Cache::$instance = Cache::connect('file', ROOT_PATH.'/tmp/cache');

// Storage::config(array(
//     'db' => Database::$instance,
//     'index_table' => 'admin_storage_index',
//     'mate_table' => 'admin_storage_mate',
//     'data_table' => 'admin_storage_data',
// ));

Model::$dir = ROOT_PATH.'/Models';

Smarty3::instance()->setTemplateDir(ROOT_PATH.'/Templates');
Smarty3::instance()->compile_dir = ROOT_PATH.'/tmp/compile/';
Smarty3::instance()->error_reporting = E_ALL ^ E_NOTICE;
Smarty3::instance()->allow_php_tag = true;

// require_once ROOT_PATH.'/Helper/Smarty.php';
// require_once ROOT_PATH.'/Helper/common.php';
// require_once ROOT_PATH.'/Helper/abstract.php';
// echo $_SERVER['REDIRECT_URL'];exit;
//print_r($_SERVER);exit;
//var_dump(function_exists('mysql_connect'));exit;
//phpinfo();exit;
try {
    $result = Controller::dispatch(@$_SERVER['REDIRECT_URL'], ROOT_PATH.'/Controllers');
} catch (Exception $e) {
    // out put 404 page
    if ($e->getCode() == 404) {
        header("HTTP/1.0 404 Not Found");
        Smarty3::instance()->display('front/404.html');
        exit;
    }
    die($e);
}

if ($result && Controller::$format == 'json') {
    echo json_encode($result);
}

?>